/**
 * Created by DingLiangLiang on 2021/12/6.
 */
import axios from 'axios'
import {saveAs} from 'file-saver'

async function getData(url) {
    let res = await axios.get(url);
    if (null != res && (res.status === 200 || res.status === 203))
        return res.data;
    else {
        console.log(res);
        return null;
    }
}

let checkLocalStorage = function () {
    if (!window.localStorage) {
        console.log('浏览器不支持本地存储');
        return false;
    } else return true;
}

export default {
    List: {
        Reaper: [],
        Enhancements: [],
        General: [],
        Iconic: [],
        Races: [],
        Destinies: [],
        All: []
    },
    getList: (name) => getData(`data/${name}.json`),
    async InitListData() {
        this.List.Reaper = await this.getList('Reaper');
        this.List.Enhancements = await this.getList('Enhancements');
        this.List.General = await this.getList('General');
        this.List.Iconic = await this.getList('Iconic');
        this.List.Races = await this.getList('Races');
        this.List.Destinies = await this.getList('Destinies');
        this.List.All.push.apply(this.List.All, this.List.Reaper);
        this.List.All.push.apply(this.List.All, this.List.Enhancements);
        this.List.All.push.apply(this.List.All, this.List.General);
        this.List.All.push.apply(this.List.All, this.List.Iconic);
        this.List.All.push.apply(this.List.All, this.List.Races);
        this.List.All.push.apply(this.List.All, this.List.Destinies);
    },
    getDes: (name) => getData(`data/des/${name}.json`),
    utilities: {
        //JSON.stringify(data)
        saveJson(name, data) {
            console.log(name, data);
            let file = new File(['\uFEFF' + JSON.stringify(data)], name + ".json", {type: "text/plain;charset=utf-8"});
            saveAs(file);
        },
        deepCopy(src) {
            return JSON.parse(JSON.stringify(src))
        },
        remove(src, val) {
            let index = src.indexOf(val);
            if (index > -1) {
                src.splice(index, 1);
            }
        },
        setLocal(name, value) {
            if (checkLocalStorage()) {
                localStorage.setItem(name, JSON.stringify(value));
            }
        },
        getLocal(name) {
            const value = localStorage.getItem(name);
            if (value) {
                return JSON.parse(value);
            } else {
                return null;
            }
        },
        delLocal(name) {
            localStorage.removeItem(name);
        },
        copyToClipboard(textToCopy) {
            // navigator clipboard 需要https等安全上下文
            if (navigator.clipboard && window.isSecureContext) {
                // navigator clipboard 向剪贴板写文本
                return navigator.clipboard.writeText(textToCopy);
            } else {
                // 创建text area
                let textArea = document.createElement("textarea");
                textArea.value = textToCopy;
                // 使text area不在viewport，同时设置不可见
                textArea.style.position = "absolute";
                textArea.style.opacity = 0;
                textArea.style.left = "-999999px";
                textArea.style.top = "-999999px";
                document.body.appendChild(textArea);
                textArea.focus();
                textArea.select();
                return new Promise((res, rej) => {
                    // 执行复制命令并移除文本框
                    document.execCommand('copy') ? res() : rej();
                    textArea.remove();
                });
            }
        }
    }
}

